package com.zhiyin.seal.service.impl;

import com.zhiyin.seal.dao.AlarmRecordDao;
import com.zhiyin.seal.dao.SealDao;
import com.zhiyin.seal.entity.AlarmRecordEntity;
import com.zhiyin.seal.entity.SealEntity;
import com.zhiyin.seal.entity.SealFingerPrintEntity;
import com.zhiyin.seal.entity.SealReplaceEntity;
import com.zhiyin.seal.entity.bo.*;
import com.zhiyin.seal.service.SealService;
import com.zhiyin.seal.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SealServiceImpl implements SealService {

    @Autowired
    SealDao sealDao;

    @Autowired
    AlarmRecordDao alarmRecordDao;

    @Override
    public SealBo getSeal(Long id) {
        return BeanUtil.copy(sealDao.selectSealById(id),SealBo.class);
    }

    @Override
    public SealBo getSealByIdAndCompany(Long id, Long companyId) {
        return BeanUtil.copy(sealDao.selectSealByIdAndCompany(id,companyId),SealBo.class);
    }

    @Override
    public SealBo getSealByMac(String mac) {
        SealEntity sealEntity = sealDao.selectSealByMac(mac);
        if(sealEntity == null){
            return null;
        }

        return BeanUtil.copy(sealEntity,SealBo.class);
    }

    @Override
    public List<SealBo> getAllSealList(String keywords) {
        return BeanUtil.copyList(sealDao.selectAllSealList(keywords),SealBo.class);
    }

    @Override
    public List<SealBo> getSealListByIdsAndCompany(List<Long> sealIds, Long companyId) {
        return BeanUtil.copyList(sealDao.selectSealListByIdsAndCompany(sealIds,companyId),SealBo.class);
    }

    @Override
    public List<SealBo> getCompanySealList(Long companyId) {
        List<SealEntity> sealEntityList = sealDao.selectCompanySealList(companyId);
        return BeanUtil.copyList(sealEntityList,SealBo.class);
    }

    @Override
    public List<SealBo> getCompanySealList2(Long companyId) {
        List<SealEntity> sealEntityList = sealDao.selectCompanySealList2(companyId);
        return BeanUtil.copyList(sealEntityList,SealBo.class);
    }

    @Override
    public List<SealBo> getApplySealList(Long userId) {
        List<SealEntity> sealEntityList = sealDao.selectApplySealList(userId);
        return BeanUtil.copyList(sealEntityList,SealBo.class);
    }

    @Override
    public List<SealBo> getMySealList(Long userId) {
        List<SealEntity> sealEntityList = sealDao.selectMySealList(userId);
        return BeanUtil.copyList(sealEntityList,SealBo.class);
    }

    @Override
    public Boolean addSeal(SealBo sealBo) {
        if(sealBo.getVersion() == null){
            sealBo.setVersion("2.0");
        }
        SealEntity sealEntity = BeanUtil.copy(sealBo,SealEntity.class);
        return sealDao.insertSeal(sealEntity) > 0;
    }

    @Override
    public Boolean addSealCompany(SealBo sealBo) {
        SealEntity sealEntity = BeanUtil.copy(sealBo,SealEntity.class);
        return sealDao.insertSealCompany(sealEntity) > 0;
    }

    @Override
    public Boolean updateSeal(SealBo sealBo) {
        return sealDao.updateSeal(BeanUtil.copy(sealBo,SealEntity.class)) > 0;
    }

    @Override
    public Boolean addSealReplace(SealReplaceBo sealReplaceBo) {
        return sealDao.insertSealReplace(BeanUtil.copy(sealReplaceBo, SealReplaceEntity.class)) > 0;
    }

    @Override
    public SealReplaceBo getSealReplaceByMac(String mac) {
        return BeanUtil.copy(sealDao.selectSealReplaceByMac(mac),SealReplaceBo.class);
    }

    @Override
    public Boolean updateSealServiceTime(Long sealId,String serviceTime) {
        return sealDao.updateSealServiceTime(sealId,serviceTime) > 0;
    }

    @Override
    public Boolean updateSealName(Long sealId, String name) {
        return sealDao.updateSealName(sealId,name) > 0;
    }

    @Override
    public Boolean updateSealForcePhotograph(Long sealId, Boolean forcePhotograph) {
        return sealDao.updateSealForcePhotograph(sealId,forcePhotograph) > 0;
    }

    @Override
    public Boolean updateSealFacePhoto(Long sealId, Boolean facePhoto) {
        return sealDao.updateSealFacePhoto(sealId,facePhoto) > 0;
    }

    @Override
    public Boolean updateSealDepartment(Long sealId, Long departmentId) {
        return sealDao.updateSealDepartment(sealId,departmentId) > 0;
    }

    @Override
    public Boolean updateSealStatus(Long sealId, Integer status) {
        return sealDao.updateSealStatus(sealId,status) > 0;
    }

    @Override
    public Boolean removeSealCompany(Long userId, Long sealId) {
        return sealDao.removeSealCompany(userId,sealId) > 0;
    }

    @Override
    public Boolean removeSealCompanyBySealId(Long sealId) {
        return sealDao.removeSealCompanyBySealId(sealId) > 0;
    }

    @Override
    public Boolean removeCompanyAllSeal(Long companyId) {
        return sealDao.deleteCompanyAllSeal(companyId) > 0;
    }

    @Override
    public Boolean uploadAlarmRecord(AlarmRecordBo alarmRecordBo) {
        return alarmRecordDao.insertAlarmRecord(BeanUtil.copy(alarmRecordBo, AlarmRecordEntity.class)) > 0;
    }

    @Override
    public Boolean removeSealAdmin(Long sealId) {
        return sealDao.deleteSealAdmin(sealId) > 0;
    }

    @Override
    public List<SealFingerPrintBo> getFingerprintList(Long companyId, Long sealId) {
        List<SealFingerPrintBo> sealFingerPrintBoList = BeanUtil.copyList(sealDao.selectFingerprintList(companyId,sealId),SealFingerPrintBo.class);
        return sealFingerPrintBoList;
    }

    @Override
    public Boolean registerFingerprint(SealFingerPrintBo sealFingerPrintBo) {
        SealFingerPrintEntity sealFingerPrintEntity = BeanUtil.copy(sealFingerPrintBo,SealFingerPrintEntity.class);
        return sealDao.insertFingerprint(sealFingerPrintEntity) > 0;
    }

    @Override
    public Boolean modifyFingerprint(SealFingerPrintBo sealFingerPrintBo) {
        SealFingerPrintEntity sealFingerPrintEntity = BeanUtil.copy(sealFingerPrintBo,SealFingerPrintEntity.class);
        return sealDao.updateFingerprint(sealFingerPrintEntity) > 0;
    }

    @Override
    public Boolean removeFingerprint(Long id,Long deleteUser) {
        return sealDao.removeFingerprint(id,deleteUser) > 0;
    }

    @Override
    public Integer getSealCount() {
        return sealDao.selectSealCount();
    }

    @Override
    public void buildSealIdsByAlarmRecordList(List<AlarmRecordBo> alarmRecordBoList, List<Long> sealIds) {
        sealIds.add(0l);
        if(alarmRecordBoList != null){
            for (AlarmRecordBo alarmRecordBo : alarmRecordBoList) {

                if(alarmRecordBo.getSealId() == null)
                    continue;
                sealIds.add(alarmRecordBo.getSealId());
            }
        }
    }

    @Override
    public void buildSealIdsByElectronicFenceList(List<ElectronicFenceDetailBo> electronicFenceDetailBoList, List<Long> sealIds) {
        sealIds.add(0l);
        if(electronicFenceDetailBoList != null){
            for (ElectronicFenceDetailBo electronicFenceBo : electronicFenceDetailBoList) {

                if(electronicFenceBo.getSealId() == null)
                    continue;
                sealIds.add(electronicFenceBo.getSealId());
            }
        }
    }
}